import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

import Pieces.BlockType;


public class GUI extends JFrame {

	private boolean 	paused 		  = false;
	private int 		buttonPressed = 0;
	
	private JButton 	playPause, newGame;
	
	//is nextPieceBox just a duplicate of nextPiece?
	private JPanel  	nextPiece, nextPieceBox, rightSide;
	private JTextField 	scoreBox, debugBox;
	
    private Board 		gameBoard;
	private BoardPanel 	board, piecePreview;
	
	//Not used...?
	private KeyListener left;

	public boolean isPaused() {
		return paused;
	}
	
	public void resetFocus(){
		this.requestFocus();
	}
	
	ActionListener playListener = new ActionListener(){
		@Override
		public void actionPerformed(ActionEvent arg0) {
				paused = !paused;
				resetFocus();
		}		
	};
	

	ActionListener newGameListener = new ActionListener(){
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// make new game
			resetFocus();
		}		
	};

	KeyListener listener = new KeyListener() {
		public void keyPressed(KeyEvent e) {
			if(e.getKeyCode() == KeyEvent.VK_DOWN){
				dumpInfo("Pressed", e);
			}
		}

		public void keyReleased(KeyEvent e) {
			dumpInfo("Released", e);
		}

		public void keyTyped(KeyEvent e) {
			//dumpInfo("Typed", e);
		}

		private void dumpInfo(String s, KeyEvent e) {
			buttonPressed = e.getKeyCode();
			System.out.println(buttonPressed);
			//scoreBox.setText(""+buttonPressed);
			//debugBox.setText(""+buttonPressed);
		}
	};

	public int getButton(){
		int temp = buttonPressed;
		buttonPressed = 0;
		return temp;
	}
	
	public void showBoard(){
		BlockType[][] brd = gameBoard.getBoard();
		for(int i = 0; i < Board.BOARD_HEIGHT; i++){
			for(int j = 0; j < Board.BOARD_WIDTH; j++){
				board.changeColor(i, j, brd[i][j].getBlockColor());
			}
		}
		
		for(int i = 1; i < 5; i++) {
			for(int j = 1; j < 5; j++) {
				BlockType next = gameBoard.getNextPiece().getCurrentBlockArray()[i-1][j-1];
				piecePreview.changeColor(i, j, next.getBlockColor());
			}
		}
		
		repaint();
	}
	
	public void updateScore(int points) {
		scoreBox.setText(Integer.toString(points));
	}


	public GUI(Board theBoard){

		super("Tetris 9000");
		gameBoard = theBoard;
		// Control Panel
		playPause = new JButton("Play / Pause");
		newGame = new JButton("New Game");
		scoreBox = new JTextField(10);
		scoreBox.setEditable(false);
		scoreBox.setText("0");
		
		playPause.addActionListener(playListener);
		newGame.addActionListener(newGameListener);
		
		JPanel controlPanel = new JPanel();
		controlPanel.setLayout(new GridLayout(20,1));
		controlPanel.add(playPause);
		controlPanel.add(newGame);
		controlPanel.add(scoreBox);

		// Main Container
		Container content = this.getContentPane();
		content.setBackground(Color.white);
		//	    content.setLayout(new BorderLayout());
		//	    content.add(controlPanel,BorderLayout.EAST);

		board = new BoardPanel();
		board.setPreferredSize(new Dimension(400, 837));
		rightSide = new JPanel();
		nextPiece = new JPanel();
		nextPiece.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		content.setLayout(new BoxLayout(content, BoxLayout.X_AXIS));
		content.add(board);
		board.setSize(board.getPreferredSize());
		board.setMinimumSize(board.getPreferredSize());
		board.setMaximumSize(board.getPreferredSize());
		
		piecePreview = new BoardPanel(6, 6);
		piecePreview.setSize(250, 250);
		piecePreview.setPreferredSize(piecePreview.getSize());
		piecePreview.setMinimumSize(piecePreview.getPreferredSize());
		piecePreview.setMaximumSize(piecePreview.getPreferredSize());

		rightSide.setLayout(new BoxLayout(rightSide, BoxLayout.Y_AXIS));
		
		nextPiece.setSize(250, 250);
		nextPiece.setPreferredSize(nextPiece.getSize());
		nextPiece.setMinimumSize(nextPiece.getPreferredSize());
		nextPiece.setMaximumSize(nextPiece.getPreferredSize());
		nextPiece.setBackground(Color.black);


		newGame.setPreferredSize(playPause.getPreferredSize());
		newGame.setSize(playPause.getPreferredSize());
		newGame.setMinimumSize(playPause.getPreferredSize());
		newGame.setMaximumSize(playPause.getPreferredSize());

		scoreBox.setPreferredSize(playPause.getPreferredSize());
		scoreBox.setSize(playPause.getPreferredSize());
		scoreBox.setMinimumSize(playPause.getPreferredSize());
		scoreBox.setMaximumSize(playPause.getPreferredSize());
		scoreBox.setBackground(Color.white);

		rightSide.add(piecePreview);
		//rightSide.add(nextPiece);
		rightSide.add(scoreBox);
		rightSide.add(Box.createVerticalGlue());
		rightSide.add(playPause);
		rightSide.add(Box.createRigidArea(new Dimension(0, 20)));
		rightSide.add(newGame);

		content.add(rightSide);

		// Frame work
		this.setSize(650, 837);
		this.addKeyListener(listener);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.requestFocus();// do this!!!!!!!!!!!
	}


	
}
